package net.dataslim.invoke;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class DataSlimInputHandler extends DataSlimInput implements InvocationHandler{

	private Object delegate;
	private Class delegateClass;

	public DataSlimInputHandler(Object delegate, Class interfaceClass)
	{
		this.delegate = delegate;
		this.delegateClass = delegate.getClass();
	}
	
	public Object invoke(Object proxy, Method method, Object[] args)
		throws Exception{
		try	{
			Method methodForInvoke = delegateClass.getMethod(method.getName(), (Class[])method
					.getParameterTypes());
			Object[] argsUnProxied = new Object[args.length];
			for(int i = 0, s = args.length; i < s; i++){
				argsUnProxied[i] = unProxied(args[i]);
			}
			Object result = methodForInvoke.invoke(delegate, argsUnProxied);
			return result;
		}catch (Throwable e){
			e.printStackTrace();
			throw new Exception("Erro:" + e.getMessage() + " Causado por: " + e.getCause());
		}
	}
	
	

}
